#include "appealcheck.h"
#include "ui_appealcheck.h"
#include"appealwindow.h"
#include<QListWidget>
#include<QDebug>
#include<QMouseEvent>
QString appealcheck::appealhistory1[999];
QString appealcheck::appealhistory2[999];
int appealcheck::appealhistoryNum = 0;

appealcheck::appealcheck(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::appealcheck)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint); // 设置窗口无边框
    // 创建一个QListWidget对象
    listWidget->setStyleSheet(
        "QListWidget {"
        "background-color: white;" // 设置 QListWidget 的背景颜色
        "border: 1px solid black;" // 设置 QListWidget 的边框
        "}"
        "QListWidget::item {"
        "padding: 5px;" // 设置项的内边距
        "background-color: gray;" // 设置项的背景颜色为灰色
        "border: 1px solid white;" // 设置项的边框，与背景颜色协调
        "color: black;" // 设置文本颜色，确保在灰色背景上的可读性
        "}"
        "QListWidget::item:hover {" // 设置鼠标悬停时的样式
        "background-color: darkgray;" // 悬停时背景颜色为深灰色
        "color: white;" // 悬停时文本颜色为白色，以增强对比度
        "}"
    );
    if(appealhistoryNum == 0){
        QString itemText = "目前还没有申诉信息";
        QListWidgetItem *item = new QListWidgetItem(itemText);
        listWidget->addItem(item);
    }
    for(int i = appealhistoryNum-1;i>=0;--i){
        QListWidgetItem *item = new QListWidgetItem(appealhistory1[i]);
        listWidget->addItem(item);
    }
    // 连接信号和槽
    connect(listWidget, &QListWidget::itemClicked, this, &appealcheck::onItemClicked);
    ui->verticalLayout->addWidget(listWidget);
}

void appealcheck::onItemClicked(QListWidgetItem *item) {
    QString text1 = item->text();
    QStringList list = text1.split(" ");
    QString text = list.at(0);
    bool ok;
    int number = text.mid(2).toInt(&ok); // 从第3个字符开始截取
    number--;
    if (ok && number >= 0 && number < appealhistoryNum) {
        appealwindow *a = new appealwindow;
        a->setText(appealhistory2[number]);
        a->show();
    }
}

appealcheck::~appealcheck()
{
    delete ui;
}

void appealcheck::on_pushButton_2_clicked()
{
    this->close();
}

void appealcheck::mousePressEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
                initialMousePosition = event->globalPos(); // 记录鼠标按下时的全局位置
                isDragging = true; // 开始拖动
            }
    }

void appealcheck::mouseMoveEvent(QMouseEvent *event){
    if (isDragging && event->buttons() & Qt::LeftButton) {
                QPoint delta = event->globalPos() - initialMousePosition; // 计算鼠标移动的距离
                move(x() + delta.x(), y() + delta.y()); // 根据鼠标移动的距离来移动窗口
                initialMousePosition = event->globalPos(); // 更新鼠标位置
            }
    }
void appealcheck::mouseReleaseEvent(QMouseEvent *event) {
        if (event->button() == Qt::LeftButton) {
            isDragging = false; // 结束拖动
        }
    }
